import urllib2
import re
import mylib
from datetime import date
from time import mktime
import pythonlibSB
import MySQLdb
import config

def get_content_url(url):
    try:
        respone = urllib2.urlopen(url)
        content = respone.read()
        return content
    except:
        return ''
def get_today_str(content):
    try:
        current_date = mylib.get_timeunix_today()
        str_time = ''
        for i in re.finditer('([0-9]+)[/]([0-9]+)[/]([0-9]+)',content):
            ngay = int(i.group(1))
            thang = int(i.group(2))
            nam = int(i.group(3))
            thistime = date(nam,thang,ngay)
            time_today = mktime(thistime.timetuple())
            if time_today == current_date:
                str_time = str(ngay)+'/'+str(thang)+'/'+str(nam)
                print str_time
                return str_time
    except:
        return ''


def parse_url_horoscope(content):
    pos1 = content.find('class="threadlisthead')
    pos2 = content.find('id="below_threadlist"',pos1)
    my_content = content[pos1:pos2]
    print my_content
    str_time_today = get_today_str(my_content)
    print str_time_today
    try:
        str_container = pythonlibSB.s_string(my_content,str_time_today,'a')
        reg = re.compile('(href=")(.*?)(")')
        if reg.search(str_container):
            url_today = str(reg.search(str_container).group(2))
            return url_today
    except:
        return ''


def insert_link_to_mysql(conn,url,ngay):
    try:
        conn.ping()
    except:
        conn = mylib.create_conn()
    cmd = "CALL insert_url_ngay('%s','%s')" % (url,int(ngay))
    cursor = conn.cursor(MySQLdb.cursors.DictCursor)
    cursor.execute(cmd)
    cursor.close()
    print "insert thanh cong"



def check_exit_link(conn,url):
    status = False
    try:
        conn.ping()
    except:
        conn = mylib.create_conn()
    get_link_str = "SELECT * FROM `url_ngay`"
    mydate = date.today()
    thisdate = int(mktime(mydate.timetuple()))
    cursor = conn.cursor(MySQLdb.cursors.DictCursor)
    cursor.execute(get_link_str)
    myresult = cursor.fetchall()
    if myresult == None:
        status = False
    for r in myresult:
        link = r['url_horoscope']
        save_date = int(r['ngay'])
        if link == url and save_date == thisdate and url != '':
            status = True
            break
    return status
        

def process_link(conn):
    mydate = date.today()
    thisdate = mktime(mydate.timetuple())
    try:
        conn.ping()
    except:
        conn = mylib.create_conn()
    content = get_content_url('http://hgth.vn/diendan/horoscope-hang-ngay/')
    url_get = parse_url_horoscope(content)
    print url_get
    if check_exit_link(conn, url_get) == False:
        insert_link_to_mysql(conn, url_get,thisdate)
    else:
        print "Link nay da ton tai"



if __name__ == "__main__":
    conn = mylib.create_conn()
    process_link(conn)
    